package com.servlet;

import com.alibaba.fastjson.JSON;
import com.dao.Impl.RechargerecordDaoImpl;
import com.dao.RechargerecordDao;
import com.entity.PageBean;
import com.entity.Rechargerecord;
import com.util.MyUtil;
import com.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value = "/rechargereCordServlet/*")
public class RechargereCordServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methon = uri.substring(uri.lastIndexOf("/") + 1);
        // 阿杰说这里使用null比较好，所以我就用了
        String target = null;
        switch (methon){
            case "findSum":
                target = findSum(request,response);
                break;
            case "addExpenseRecord":
                target = addExpenseRecord(request,response);
                break;
            case "findRechargeRecord":
                target = findRechargeRecord(request,response);
                break;
            case "insert":
                target = insert(request,response);
                break;
            default:break;
        }
        //使用两个判断
        if (target !=null && target != ""){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }


    /**
     * 新增会员时增加充值记录
     * @param request
     * @param response
     * @return
     */
    private String insert(HttpServletRequest request, HttpServletResponse response) {
        Rechargerecord rc=new Rechargerecord();
        TransBean.populate(rc,request.getParameterMap());
        rc.setRechargeAmount(Double.parseDouble(request.getParameter("amount")));
        rc.setAfterAmount(Double.parseDouble(request.getParameter("amount")));
        RechargerecordDao dao=new RechargerecordDaoImpl();
        dao.insert(rc);
        return null;
    }

    // 充值统计图
    private String findSum(HttpServletRequest request,HttpServletResponse response){
        RechargerecordDao rechargerecordDao = new RechargerecordDaoImpl();
        Map map = rechargerecordDao.findsum();
        for (int i = 1; i <= 12; i++) {
            if (map.get(i) == null){
                map.put(i,0);
            }
        }
        request.setAttribute("remap", JSON.toJSONString(map));
        return "/statistics/recharge.jsp";
    }

    /**
     * 增加充值记录
     * @param request
     * @param response
     * @return
     */

    private String addExpenseRecord(HttpServletRequest request,HttpServletResponse response){
        Rechargerecord rechargerecord=new Rechargerecord();
        TransBean.populate(rechargerecord,request.getParameterMap());
        RechargerecordDao dao=new RechargerecordDaoImpl();
        dao.addRechargeRecord(rechargerecord);
        return null;
    }

    /**
     * 查询会员充值记录
     *
     * @param request
     * @param response
     * @return
     */
    private String findRechargeRecord(HttpServletRequest request,HttpServletResponse response){
        PageBean<Map> pageBean=new PageBean();
        pageBean.setPageNo(Integer.parseInt(request.getParameter("pageBean[pageNo]")));
        pageBean.setPageSize(Integer.parseInt(request.getParameter("pageBean[pageSize]")));
        int cardId= Integer.parseInt(request.getParameter("cardId"));
        RechargerecordDao dao=new RechargerecordDaoImpl();
        dao.findRechargeRecord(pageBean,cardId);
        MyUtil.jsonWriter(response,pageBean);
        return null;
    }


}
